package com.hk.dao;

import com.hk.entity.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class EmpDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public Emp SelectById(int id){
        String sql="select id,name,salary from emp where id=?";
         Emp emp=jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(Emp.class),id);
        return emp;
    }
    public List<Emp> SelectEmps(){
        String sql="select id,name,salary from emp";
        List<Emp> emps= jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Emp.class));
        return emps;
    }
    public List SelectEmpsByList(){
        String sql="select id,name,salary from emp";
        List<Map<String,Object>> listemps=jdbcTemplate.queryForList(sql);
        return listemps;
    }
    public int DeleteById(int id){
        String sql="delete from emp where id=?";
        int result=jdbcTemplate.update(sql,id);
        return result;
    }
    public int AddEmp(Emp emp){
        String sql="insert into emp(id,name,salary) values(?,?,?) ";
        int result=jdbcTemplate.update(sql,emp.getId(),emp.getName(),emp.getSalary());
        return result;
    }
    public int UpdateEmp(String newname){
        String sql="update emp set name=? where id=2";
        int result=jdbcTemplate.update(sql,newname);
        return result;
    }
}
